package IO流;

import org.junit.Test;
import java.io.*;

/*
    转换流
         转换流提供了在字节流和字符流之间的转换
         Java API提供了两个转换流:
             InputStreamReader:将InputStream转换为Reader
             OutputStreamWriter:将Writer转换为OutputStream
         字节流中的数据都是字符时,转成字符流操作更高效
         很多时候使用转换流来处理文件乱码问题,实现编码和解码的功能

    InputStreamReader
         实现将字节的输入流按指定字符集转换为字符的输入流
         需要和InputStream"套接"
         构造器
             public InputStreamReader(InputStream in)
             public InputStreamReader(InputStream in,String charsetName)

    OutputStreamWriter
         实现将字符的输出流按指定字符集转换为字节的输出流
         需要和OutputStream"套接"
         构造器
              public OutputStreamWriter(OutputStream out)
              public OutputStreamWriter(OutputStream out,String charsetName)

        解码:字节、字节数组  --> 字符数组、字符串
        编码:字符数组、字符串 --> 字节、字节数组
 */
public class IO04 {

    @Test
    public void test1(){

        InputStreamReader inputStreamReader = null;

        try {
            FileInputStream inputStream = new FileInputStream("E:\\FileTest\\Hello.txt");
            inputStreamReader = new InputStreamReader(inputStream,"UTF-8");//如果不指定字符集,则使用系统默认的字符集

            char[] array = new char[1024];
            int len;

            while ((len = inputStreamReader.read(array)) != -1){
                System.out.println(new String(array,0,len));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inputStreamReader != null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //转换编码集
    @Test
    public void test2(){
        File src = new File("E:\\FileTest\\Hello.txt");
        File desc = new File("E:\\FileTest\\Hello2.txt");

        InputStreamReader input = null;
        OutputStreamWriter output = null;

        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(src));
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(desc));

            input = new InputStreamReader(bufferedInputStream,"UTF-8");
            output = new OutputStreamWriter(bufferedOutputStream,"GBK");

            char[] array = new char[1024];
            int len;

            while ((len = input.read(array)) != -1){
                output.write(array,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (output != null){
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (input != null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
